package summary;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/7 14:51
 * https://leetcode-cn.com/problems/minimum-size-subarray-sum/
 */
public class Title209 {

    public static int minSubArrayLen(int target, int[] nums) {
        int sum = 0, left = 0;
        int minLen = nums.length + 1;
        for (int right = 0; right < nums.length; right++) {
            sum += nums[right];
            while (sum >= target) {
                minLen = Math.min(right - left + 1, minLen);
                sum -= nums[left++];
            }
        }
        return minLen == nums.length ? 0 : minLen;
    }


    public int minSubArrayLen1(int target, int[] nums) {
        int sum = 0;
        int left = 0;
        int min = nums.length + 1;
        for (int right = 0; right < nums.length; right++) {
            sum += nums[right];
            while (sum >= target) {
                min = Math.min(min, right - left + 1);
                sum -= nums[left];
                left++;
            }
        }
        return min == nums.length + 1 ? 0 : min;
    }


    public static void main(String[] args) {

        int[] arr = {1,2,3,4,5};
        System.out.println(minSubArrayLen(15, arr));
    }
}
